package service
{
	import com.maclema.mysql.Connection;
	import com.maclema.mysql.Field;
	import com.maclema.mysql.MySqlToken;
	import com.maclema.mysql.ResultSet;
	import com.maclema.mysql.Statement;
	import com.maclema.mysql.events.MySqlErrorEvent;
	
	import data.db.MapDB;
	import data.db.MonsterDB;
	import data.db.NpcDB;
	
	import events.DBEvent;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	
	import mx.controls.Alert;
	import mx.rpc.AsyncResponder;
	
	import service.event.ServiceEvent;
	
	import utils.EventManager;

	/**
	 * 
	 * 与数据库交互部分
	 * */
	public class MapService
	{
		private var con : Connection = null;
		
		private static var _instance:MapService;
		
		public function MapService()
		{
		}
		
		public static function getInstance():MapService{
			if(_instance == null){
				_instance = new MapService();
			}
			return _instance;
		}
		
		/**
		 * 连接mysql数据库
		 * */
		public function connect(ip:String = "10.0.0.207",userName:String = "root",password:String = "liumang",dbname:String = "webgame"):void{
			con = new Connection(ip, 3306, userName, password, dbname);
			con.addEventListener(Event.CONNECT,onConnectComplate,false,0,true);
			con.addEventListener(MySqlErrorEvent.SQL_ERROR,onConnectError);
			con.addEventListener(IOErrorEvent.IO_ERROR,onConnectError);
			con.connect();
		}
		
		private function onConnectComplate(event:Event):void{
			EventManager.getInstance().dispatchEvent(new ServiceEvent(ServiceEvent.CONNECT_COMPLATE,true));
		}
		private function onConnectError(event:Event):void{
			// 数据库连接失败
			EventManager.getInstance().dispatchEvent(new ServiceEvent(ServiceEvent.CONNECT_COMPLATE,false));
		}
		/**
		 * @param sql执行语句
		 * @param callBackFun回调语句函数
		 * */
		public function executeSql(sql:String,callFun:Function):void{
			var statement : Statement = con.createStatement();
			var token : MySqlToken = statement.executeQuery(sql);
			token.addResponder(new AsyncResponder(
				function(data : Object, token : Object) : void {
					var rs : ResultSet = ResultSet(data);
					callFun(rs);
				},
				function(data : Object, token : Object) : void {
					// 执行语句失败
					Alert.show("数据通讯失败！请检查您的网络环境！","提示！");
				}, 
				token));
		}
		public function queryMaps(rs:ResultSet):void{
			var cloumns:Array = rs.getColumns();
			var mapDB:MapDB = null;
			MapData.db_maps.clear();
			while(rs.next()){
				mapDB = new MapDB();
				for(var i:int = 0; i < cloumns.length ; i++){
					var filed:Field = cloumns[i];
					switch(i){
						case 0: mapDB.id = rs.getInt(filed.getRealName()); break;
						case 1: mapDB.name = rs.getString(filed.getRealName()); break;
						case 2: mapDB.desc = rs.getString(filed.getRealName()); break;
						case 3: mapDB.maptype = rs.getInt(filed.getRealName()); break;
						case 4: mapDB.pk_level = rs.getInt(filed.getRealName()); break;
						case 5: mapDB.if_team = rs.getInt(filed.getRealName()); break;
						case 6: mapDB.user_limit = rs.getInt(filed.getRealName()); break;
						case 7: mapDB.if_first_map = rs.getInt(filed.getRealName()); break;
						case 8: mapDB.pre_map_id = rs.getInt(filed.getRealName()); break;
						case 9: mapDB.next_map_id = rs.getInt(filed.getRealName()); break;
						case 10: mapDB.user_level = rs.getInt(filed.getRealName()); break;
						case 11: mapDB.suggest_lowest_level = rs.getInt(filed.getRealName()); break;
						case 12: mapDB.suggest_highest_level = rs.getInt(filed.getRealName()); break;
						case 13: mapDB.daily_limit = rs.getInt(filed.getRealName()); break;
						case 14: mapDB.status = rs.getInt(filed.getRealName()); break;
						case 15: mapDB.resource_uri = rs.getString(filed.getRealName()); break;
						case 16: mapDB.version = rs.getInt(filed.getRealName()); break;
						case 17: mapDB.normal_x = rs.getInt(filed.getRealName()); break;
						case 18: mapDB.normal_y = rs.getInt(filed.getRealName()); break;
						case 19: mapDB.normal_asset = rs.getInt(filed.getRealName()); break;
						case 20: mapDB.music = rs.getString(filed.getRealName()); break;
						case 21: mapDB.page = rs.getInt(filed.getRealName()); break;
						case 22: mapDB.xml_uri = rs.getString(filed.getRealName()); break;
					}
				}
				MapData.db_maps.put(mapDB.id,mapDB);
			}
			// 发送读取map完毕的事件
			EventManager.getInstance().dispatchEvent(new DBEvent(DBEvent.QUERY_MAPS));
		}
		public function queryNpcs(rs:ResultSet):void{
			var cloumns:Array = rs.getColumns();
			var npcDB:NpcDB = null;
			MapData.db_npcs.clear();
			while(rs.next()){
				npcDB = new NpcDB();
				for(var i:int = 0; i < cloumns.length ; i++){
					var filed:Field = cloumns[i];
					switch(i){
						case 0: npcDB.id = rs.getInt(filed.getRealName()); break;
						case 1: npcDB.name = rs.getString(filed.getRealName()); break;
						case 2: npcDB.function_type = rs.getString(filed.getRealName()); break;
						case 3: npcDB.talk = rs.getString(filed.getRealName()); break;
						case 4: npcDB.function_task = rs.getString(filed.getRealName()); break;
						case 5: npcDB.function_duplicate = rs.getString(filed.getRealName()); break;
						case 6: npcDB.body_id = rs.getInt(filed.getRealName()); break;
						case 7: npcDB.version = rs.getInt(filed.getRealName()); break;
					}
				}
				MapData.db_npcs.put(npcDB.id,npcDB);
			}
			EventManager.getInstance().dispatchEvent(new DBEvent(DBEvent.QUERY_NPCS));
		}
		public function queryMonsters(rs:ResultSet):void{
			var cloumns:Array = rs.getColumns();
			var monsterDB:MonsterDB = null;
			MapData.db_monsters.clear();
			while(rs.next()){
				monsterDB = new MonsterDB();
				for(var i:int = 0; i < cloumns.length ; i++){
					var filed:Field = cloumns[i];
					switch(i){
						case 0: monsterDB.id = rs.getInt(filed.getRealName()); break;
						case 1: monsterDB.name = rs.getString(filed.getRealName()); break;
						case 2: monsterDB.level = rs.getInt(filed.getRealName()); break;
						case 17: monsterDB.ai_type = rs.getInt(filed.getRealName()); break;
						case 31: monsterDB.body_id = rs.getInt(filed.getRealName()); break;
					}
				}
				MapData.db_monsters.put(monsterDB.id,monsterDB);
			}
			EventManager.getInstance().dispatchEvent(new DBEvent(DBEvent.QUERY_MONSTERS));
			
		}
		public function excuteUpdateSql(sql:String,mapName:String,mapID:int,callBackFun:Function):void{
			var st:Statement = con.createStatement();
			st.sql = sql;
			st.setString(1,mapName);
			st.setNumber(2,mapID);
			
			var token:MySqlToken = st.executeQuery();
			token.addResponder(new AsyncResponder(
				function (data:Object, token:Object):void {
					callBackFun();
				},
				function (info:Object, token:Object):void {
					Alert.show("地图资源数据保存数据库失败！请检查您的网络环境！","提示！");
				},
				token
			));
		}
	}
}